package xyz.ck.commons.interval;

import junit.framework.TestCase;

import org.junit.Test;

public class DoubleContinousIntervalUnitTestCase {
	/**
	 * [1.0,10.0)
	 */
	public static Interval<Double> a = new ContinousIntervalUnit<Double>(true,
			1d, 10d, false);
	/**
	 * [1.0,10.0]
	 */
	public static Interval<Double> b = new ContinousIntervalUnit<Double>(true,
			1d, 10d, true);;

	/**
	 * (1.0,10.0)
	 */
	public static Interval<Double> c = new ContinousIntervalUnit<Double>(false,
			1d, 10d, false);;

	/**
	 * (-10.0,1.0)
	 */
	public static Interval<Double> d = new ContinousIntervalUnit<Double>(true,
			-10d, 1d, true);

	/**
	 * [1.0,1.0 ]
	 */
	public static Interval<Double> e = new ContinousIntervalUnit<Double>(true,
			1d, 1d, true);

	/**
	 * [10.0,15.0]
	 */
	public static Interval<Double> f = new ContinousIntervalUnit<Double>(true,
			10d, 15d, true);

	/**
	 * (10.0,15.0)
	 */
	public static Interval<Double> g = new ContinousIntervalUnit<Double>(false,
			10d, 15d, false);

	/**
	 * [11.0,22.0]
	 */
	public static Interval<Double> h = new ContinousIntervalUnit<Double>(true,
			11d, 22d, true);

	/**
	 * [-11,1)
	 */
	public static Interval<Double> i = new ContinousIntervalUnit<Double>(true,
			-11d, -1d, true);

	/**
	 * (4d,7d]
	 */
	public static Interval<Double> j = new ContinousIntervalUnit<Double>(false,
			4d, 7d, true);

	@Test
	public void testIntersect() {

		TestCase.assertEquals(String.format("%s intersect %s", a, b), a,
				a.intersect(b));

		TestCase.assertEquals(String.format("%s intersect %s", a, c), c,
				a.intersect(c));

		TestCase.assertEquals(String.format("%s intersect %s", a, d), null,
				a.intersect(d));

		TestCase.assertEquals(String.format("%s intersect %s", a, e), e,
				a.intersect(e));

		TestCase.assertEquals(String.format("%s intersect %s", a, f), null,
				a.intersect(f));

		TestCase.assertEquals(String.format("%s intersect %s", b, f),
				new ContinousIntervalUnit<Double>(true, 10d, 10d, false),
				a.intersect(f));

		TestCase.assertEquals(String.format("%s intersect %s", b, g), null,
				a.intersect(g));

		TestCase.assertEquals(String.format("%s intersect %s", b, h), null,
				a.intersect(h));

		TestCase.assertEquals(String.format("%s intersect %s", b, i), null,
				a.intersect(i));

		TestCase.assertEquals(String.format("%s intersect %s", b, j), j,
				a.intersect(j));
	}

	@Test
	public void testIsContains() {

		TestCase.assertEquals(String.format("%s isContains %s", a, b), false,
				a.isContains(b));

		TestCase.assertEquals(String.format("%s isContains %s", b, a), true,
				b.isContains(a));

		TestCase.assertEquals(String.format("%s isContains %s", a, c), true,
				a.isContains(c));

		TestCase.assertEquals(String.format("%s isContains %s", a, d), false,
				a.isContains(d));

		TestCase.assertEquals(String.format("%s isContains %s", b, f), false,
				a.isContains(f));

		TestCase.assertEquals(String.format("%s isContains %s", a, g), false,
				a.isContains(g));

		TestCase.assertEquals(String.format("%s isContains %s", a, j), true,
				a.isContains(j));
	}

	@Test
	public void testIsIn() {

		TestCase.assertEquals(String.format("%s isIn %s", a, 0d), false,
				a.isIn(0d));

		TestCase.assertEquals(String.format("%s isIn %s", a, 1d), true,
				a.isIn(1d));

		TestCase.assertEquals(String.format("%s isIn %s", a, 10d), false,
				a.isIn(10d));

		TestCase.assertEquals(String.format("%s isIn %s", a, 11d), false,
				a.isIn(11d));

	}

	@Test
	public void testIsIntersect() {
		TestCase.assertEquals(String.format("%s isIn %s", a, b), true,
				a.isIntersect(b));

		TestCase.assertEquals(String.format("%s isIn %s", a, c), true,
				a.isIntersect(c));

		TestCase.assertEquals(String.format("%s isIn %s", a, d), true,
				a.isIntersect(d));

		TestCase.assertEquals(String.format("%s isIn %s", a, e), true,
				a.isIntersect(e));

		TestCase.assertEquals(String.format("%s isIn %s", a, f), false,
				a.isIntersect(f));

		TestCase.assertEquals(String.format("%s isIn %s", b, f), true,
				b.isIntersect(f));

		TestCase.assertEquals(String.format("%s isIn %s", a, h), false,
				a.isIntersect(h));

		TestCase.assertEquals(String.format("%s isIn %s", a, i), false,
				a.isIntersect(i));

		TestCase.assertEquals(String.format("%s isIn %s", a, j), true,
				a.isIntersect(j));
	}

	@Test
	public void testMinus() {
		TestCase.assertEquals(String.format("%s intersect %s", a, b), null,
				a.minus(b));

		TestCase.assertEquals(String.format("%s intersect %s", b, a),
				new ContinousIntervalUnit<Double>(true, 10d, 10d, true),
				b.minus(a));

		TestCase.assertEquals(String.format("%s intersect %s", a, d), a,
				a.minus(d));

		TestCase.assertEquals(String.format("%s intersect %s", a, e), c,
				a.minus(e));

		TestCase.assertEquals(String.format("%s intersect %s", a, f), a,
				a.minus(f));

		TestCase.assertEquals(String.format("%s intersect %s", b, c),
				IntervalGroup.instance(new ContinousIntervalUnit<Double>(true,
						1.0, 1.0, true), new ContinousIntervalUnit<Double>(
						true, 10d, 10d, true)), b.minus(c));

		TestCase.assertEquals(String.format("%s intersect %s", a, j),
				IntervalGroup.instance(new ContinousIntervalUnit<Double>(true,
						1.0, 4.0, true), new ContinousIntervalUnit<Double>(
						false, 7d, 10d, true)), a.minus(j));
	}

	@Test
	public void testUnion() {
		TestCase.assertEquals(String.format("%s union %s", a, b), b, a.union(b));

		TestCase.assertEquals(String.format("%s union %s", c, e),
				new ContinousIntervalUnit<Double>(true, 1d, 10d, false),
				c.union(e));

		TestCase.assertEquals(String.format("%s union %s", a, d),
				new ContinousIntervalUnit<Double>(false, -10d, 10d, false),
				a.union(d));

		TestCase.assertEquals(String.format("%s union %s", a, f),
				new ContinousIntervalUnit<Double>(true, 1d, 15d, true),
				a.union(f));

		TestCase.assertEquals(String.format("%s union %s", b, f),
				new ContinousIntervalUnit<Double>(true, 1d, 15d, true),
				b.union(f));

		TestCase.assertEquals(String.format("%s union %s", a, g), IntervalGroup
				.instance(new ContinousIntervalUnit<Double>(true, 1d, 10d,
						false), new ContinousIntervalUnit<Double>(false, 10d,
						15d, false)), a.union(g));

		TestCase.assertEquals(String.format("%s union %s", a, h), IntervalGroup
				.instance(new ContinousIntervalUnit<Double>(true, 1d, 10d,
						false), new ContinousIntervalUnit<Double>(true, 11d,
						22d, true)), a.union(h));

		TestCase.assertEquals(String.format("%s union %s", a, j), a, a.union(j));
	}

}
